package xxl.signals.base.valuelists 
{
	import flash.events.Event;
	/**
	 * ...
	 * @author XXL
	 */
	public class EventValueListAscend 
	{
		public static const NIL:EventValueListAscend = new EventValueListAscend(null, null);
		
		public var first:EventValueListAscend = null;
		public var head:EventValueListAscend;
		public var tail:Event;
		public var nonEmpty:Boolean = false;
		
		public function EventValueListAscend(tail:Event,head:EventValueListAscend = null) 
		{
			if (!head && !tail)
			{
				if (NIL)
				{
					throw new ArgumentError("tail不能为空");
				}
				nonEmpty = false;
			}
			else if (!tail)
			{
				throw new ArgumentError("tail不能为空");
			}
			else
			{
				this.head = head || NIL;
				this.tail = tail;
				nonEmpty = true;
			}
		}
		
		public function append(event:Event):EventValueListAscend
		{
			const headList:EventValueListAscend = new EventValueListAscend(event);
			if (first == null)
			{
				headList.first = headList;
				return headList;
			}
			else if (first.head.nonEmpty)
			{
				throw new ArgumentError("不能从中间追加");
			}
			else
			{
				first.head = headList;
				first = headList;
				return this;
			}
		}
		
		public function get length():int
		{
			if (!nonEmpty)
			{
				return 0;
			}
			if (head == NIL)
			{
				return 1;
			}
			var result:int = 0;
			var p:EventValueListAscend = this;
			while (p.nonEmpty)
			{
				++result;
				p = p.head;
			}
			return result;
		}
		
	}

}